package handels

import (
	"bald-strong-mgr/cache"
	"bald-strong-mgr/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func BootAdd(c *gin.Context) {
	boot_from := c.PostForm("boot_from")
	boot_name := c.PostForm("boot_name")
	boot_node := c.PostForm("boot_node")
	if boot_from == "" || boot_name == "" || boot_node == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Boot{
		BootForm: boot_from,
		BootName: boot_name,
		BootNode: boot_node,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}
func BootOrderAdd(c *gin.Context) {
	bid, _ := strconv.Atoi(c.PostForm("bid"))
	order_to := c.PostForm("order_to")
	order_node := c.PostForm("order_node")
	order_price := c.PostForm("order_price")
	order_age := c.PostForm("order_age")
	if order_to == "" || bid == 0 || order_node == "" || order_price == "" || order_age == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.BootOrder{
		Bid:        bid,
		OrderTo:    order_to,
		OrderNode:  order_node,
		OrderPrice: order_price,
		OrderAge:   order_age,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}
func BootOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	var list []models.BootOrderList
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Select(
				"boot_orders.id as OrderID",
				"boots.boot_form as BootForm",
				"boots.boot_name as BootName",
				"boots.boot_node as BootNode",
				"boot_orders.order_to as OrderTo",
				"boot_orders.order_node as OrderNode",
				"boot_orders.order_price as OrderPrice",
				"boot_orders.order_age as OrderAge",
			).
			Table("boot_orders").
			Joins("LEFT JOIN boots ON boots.id = boot_orders.bid").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var count int64
	models.DB.Model(&models.BootOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"count": count,
	})
	return
}
func BootOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list models.BootOrderList
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Select(
				"boot_orders.id as OrderID",
				"boots.boot_form as BootForm",
				"boots.boot_name as BootName",
				"boots.boot_node as BootNode",
				"boot_orders.order_to as OrderTo",
				"boot_orders.order_node as OrderNode",
				"boot_orders.order_price as OrderPrice",
				"boot_orders.order_age as OrderAge",
			).
			Table("boot_orders").
			Joins("LEFT JOIN boots ON boots.id = boot_orders.bid").
			Where("boot_orders.id = ?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
